﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

using Elegant.Ui;
using iTextSharp.text.pdf;

namespace iPdf
{
    partial class MainForm : Form
    {
        #region Event handlers

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            TataRibbonPage.InstallHandlersForActor(
                Actor_ItemStarted, Actor_ItemFinished, Actor_AllItemsFinished);

            Grid.Load();

            UpdateRibbon();

            tabPageChanging = false;
            InstallIdleTimer();
        }


        private void MainForm_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && Busy)
            {
                e.Cancel = true;
                ShowMainWindow(false);
                ShowMinimizeNotification();
            }
            else
            {
                e.Cancel = false;//to make sure that the form will be closed normally
                Save();
                idleTimer.Dispose();
            }
        }

        /// <summary>
        /// Exit menu item handler of the context menu of NotifyIcon
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!Busy)
                Application.Exit();
        }

        /// <summary>
        /// Show menu item handler of the context menu of NotifyIcon
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (IsRunningInBackground)
            {
                ShowMainWindow(true);
            }
        }

        /// <summary>
        /// Double click handler of the context menu of NotifyIcon
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NotifyIcon_Click(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ShowToolStripMenuItem_Click(sender, e);
            }
        }

        #endregion

        #region Command handlers
        private void AddItems_Executed(object sender, CommandExecutedEventArgs e)
        {
            AddItems((Elegant.Ui.Command)sender);
        }

        private void Start_Executed(object sender, CommandExecutedEventArgs e)
        {
            CurrentPage.StartAction();
        }

        private void Stop_Executed(object sender, CommandExecutedEventArgs e)
        {
            UpdateStatusBar("Cancelling action, please wait...");
            cancelAction = true;
        }

        private void RemoveSelection_Executed(object sender, CommandExecutedEventArgs e)
        {
            Grid.RemoveSelection();

            //cause the Id column needs to be changed, we call UpdateColumns to do so
            Grid.UpdateCells();
        }

        private void RemoveAll_Executed(object sender, CommandExecutedEventArgs e)
        {
            Grid.RemoveAll();
        }

        private void MoveUp_Executed(object sender, CommandExecutedEventArgs e)
        {
            Grid.MoveUpSelection();

            //cause the Id column needs to be changed, we call UpdateColumns to do so
            Grid.UpdateCells();
        }

        private void MoveDown_Executed(object sender, CommandExecutedEventArgs e)
        {
            Grid.MoveDownSelection();

            //cause the Id column needs to be changed, we call UpdateColumns to do so
            Grid.UpdateCells();
        }

        private void HowToReportBugs_Executed(object sender, CommandExecutedEventArgs e)
        {
            string howToReportBugsPath = Path.Combine(Application.StartupPath,
                                                                Global.HowToReportBugsFileName);
            Helper.StartFile(howToReportBugsPath);
        }

        private void Exit()
        {
            this.Close();
        }

        private void Exit_Executed(object sender, CommandExecutedEventArgs e)
        {
            Exit();
        }

        private void IdleTimer_Tick(object sender, EventArgs e)
        {
            ImportQueuedTask();
            UpdatePrintingRelatedControls();

            if (!PdfCreator.Initialized)
            {
                PdfCreator.Initialize();
            }
        }

        private void PageSize_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.PageSize.Text))
            {
                return;
            }

            GraphicsUnit unit = this.PageSize.Text.EndsWith("pt") ? 
                GraphicsUnit.Point : GraphicsUnit.Inch;

            string sizeText = string.Empty;
            SizeF size = (SizeF)this.PageSize.Tag;
            string by = " X ";
            switch (unit)
            {
                case GraphicsUnit.Inch:
                    sizeText = size.Width.ToString() + by + size.Height.ToString() + " pt";
                    break;

                case GraphicsUnit.Point:
                    sizeText = UnitConverter.PointsToInches(size.Width).ToString() + by + 
                        UnitConverter.PointsToInches(size.Height).ToString() + " in";
                    break;
            }

            this.PageSize.Text = sizeText;
        }

        private void SessionsPanel_MouseEnter(object sender, EventArgs e)
        {
            if (this.sessionsPanel.State == Stepi.UI.ExtendedPanelState.Collapsed)
            {
                this.sessionsPanel.Expand();
            }
        }

        #endregion
    }
}
